package jee.boot.module.bpm.table.mysql.model;

import jee.boot.common.utils.StringUtils;

/**
 * 窗体中的字段属性
 */
public abstract class FormField {
    protected String fieldType;//字段类型
    protected String columnType;//列类型
    protected String fieldName;//字段名称
    protected String columnName;//列名称
    protected Object defaultValue;//默认值
    protected Boolean nullable ;//是否可为空
    protected Integer length;//长度
    protected Integer accuracy;//精度
    protected String comment;//注释
    protected String dictType;//字典类型(如果有就填,没有就空着就行)
    //-------------操作类属性
    protected Boolean create;//是否创建这个字段
    //-------------数据格式(仅限导出时候)
    protected String formatter;//格式化数据
    protected Boolean primaryKey;//是否为主键


    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public String getColumnType() {
        return columnType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public Object getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(Object defaultValue) {
        this.defaultValue = defaultValue;
    }

    public Boolean getNullable() {
        // 如果是主键,则不允许为空
        if(primaryKey!=null && primaryKey){
            nullable=false;
        }
        return nullable;
    }

    public void setNullable(Boolean nullable) {
        this.nullable = nullable;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(Integer accuracy) {
        this.accuracy = accuracy;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
        if (StringUtils.isNotBlank(getFieldName())) {
            setColumnName(StringUtils.toUnderScoreCase(getFieldName()));//驼峰转下划线设置表名
        }
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public Boolean getCreate() {
        return create;
    }

    public void setCreate(Boolean create) {
        this.create = create;
    }

    public String getDictType() {
        return dictType;
    }

    public void setDictType(String dictType) {
        this.dictType = dictType;
    }

    public String getFormatter() {
        return formatter;
    }

    public void setFormatter(String formatter) {
        this.formatter = formatter;
    }

    public Boolean getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(Boolean primaryKey) {
        this.primaryKey = primaryKey;
    }

    /**
     * 新建表字段的sql
     *
     * @return
     */
    public abstract String getCreateColumn();

    /**
     * 插入字段的sql
     *
     * @return
     */
    public String getAddColumn() {
        StringBuilder sql = new StringBuilder();
        sql.append("ADD COLUMN").append(" ");
        sql.append(getCreateColumn());
        return sql.toString();
    }

    /**
     * 变更字段的sql
     *
     * @return
     */
    public String getModifyColumn(){
        StringBuilder sql = new StringBuilder();
        sql.append("MODIFY COLUMN").append(" ");
        sql.append(getCreateColumn());
        return sql.toString();
    }

    /**
     * 删除字段的sql
     *
     * @return
     */
    public  String getDropColumn(){
        StringBuilder sql = new StringBuilder();
        sql.append("DROP COLUMN").append(" ");
        sql.append(getColumnName());
        return sql.toString();
    }
}
